/**
 * Copyright(C) 2008 Matthias Braunhofer
 * All rights reserved
 *
 * Created on: 30.08.2008
 */

package it.cums.ui.editors.order;

import it.cums.core.model.Invoice;
import it.cums.core.model.Invoices;
import it.cums.core.model.Order;
import it.cums.ui.localization.Messages;
import it.cums.ui.printing.PrintingDesign;
import it.cums.ui.wizards.InvoiceWizard;
import it.cums.core.util.Converter;
import it.cums.core.util.InvoiceManager;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import net.sf.paperclips.PaperClips;
import net.sf.paperclips.PrintJob;

import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.printing.PrintDialog;
import org.eclipse.swt.printing.PrinterData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

public class InvoicesEditTable {
    private Order order;
    private Table table;
    private TableViewer tableViewer;
    private Invoices invoices;
    
    private final static String[] COLUMN_HEADINGS = {Messages.InvoicesEditTable_0, Messages.InvoicesEditTable_1, Messages.InvoicesEditTable_2, Messages.InvoicesEditTable_3, Messages.InvoicesEditTable_4, Messages.InvoicesEditTable_5};
   
    public InvoicesEditTable(Order order) {
    	this.order = order;
    }
    
    protected Control createContents(Composite parent) {
      	Composite composite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout(1, false);
		layout.verticalSpacing = 10;
		composite.setLayout(layout);			
		GridData data = new GridData(GridData.FILL_HORIZONTAL);
		data.horizontalSpan = 3;
		data.heightHint = 160;
		data.grabExcessHorizontalSpace = true;
		composite.setLayoutData(data);
		
		Composite buttonComposite = new Composite(composite, SWT.NONE);
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 4;
        buttonComposite.setLayout(gridLayout);
		
		Button addButton = new Button(buttonComposite, SWT.PUSH);
        addButton.setText(Messages.InvoicesEditTable_6);
        addButton.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
            	InvoiceWizard wizard = new InvoiceWizard(order);
				WizardDialog dialog = new WizardDialog(table.getShell(), wizard);
				dialog.open();
            }
        });
        
        Button editButton = new Button(buttonComposite, SWT.PUSH);
        editButton.setText(Messages.InvoicesEditTable_7);
        editButton.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
            	ISelection selection = tableViewer.getSelection();
                if (selection instanceof IStructuredSelection) {
                	Object obj = ((IStructuredSelection)selection).getFirstElement();
                	if (obj instanceof Invoice) {
                		InvoiceWizard wizard = new InvoiceWizard((Invoice)obj);
	    				WizardDialog dialog = new WizardDialog(table.getShell(), wizard);
	    				dialog.open();
                	}
                }
            }
        });
        
        Button removeButton = new Button(buttonComposite, SWT.PUSH);
        removeButton.setText(Messages.InvoicesEditTable_8);
        removeButton.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                ISelection selection = tableViewer.getSelection();
                if (selection instanceof IStructuredSelection) {
                	Object obj = ((IStructuredSelection)selection).getFirstElement();
                	if (obj instanceof Invoice) {
	                    invoices.removeInvoice((Invoice)obj);
                	}
                }
            }
        });
        
        Button printButton = new Button(buttonComposite, SWT.PUSH);
        printButton.setText(Messages.InvoicesEditTable_9);
        printButton.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
            	ISelection selection = tableViewer.getSelection();
                if (selection instanceof IStructuredSelection) {
                	Object obj = ((IStructuredSelection)selection).getFirstElement();
                	if (obj instanceof Invoice) {
                		Invoice invoice = (Invoice)obj;
                		PrintJob printJob = new PrintJob(Messages.InvoicesEditTable_10 + invoice.getId(), PrintingDesign.createPrint(invoice));
                		PrintDialog dialog = new PrintDialog(Display.getDefault().getActiveShell(), SWT.NONE);
                        PrinterData printerData = dialog.open();
                        if (printerData != null) {
                        	PaperClips.print(printJob, printerData);
                        }
                	}
                }
            }
        });
		
		table = new Table(composite, SWT.BORDER | SWT.V_SCROLL | SWT.SINGLE | SWT.FULL_SELECTION);
		table.setLinesVisible(true);
        table.setHeaderVisible(true);
        data = new GridData(GridData.FILL_BOTH);
        table.setLayoutData(data);
         
        TableLayout tableLayout = new TableLayout();
        table.setLayout(tableLayout);
        
        tableLayout.addColumnData(new ColumnWeightData(15, 118, true));
        TableColumn column = new TableColumn(table, SWT.NONE);
        column.setText(COLUMN_HEADINGS[0]);
        column.setAlignment(SWT.LEFT);
        
        tableLayout.addColumnData(new ColumnWeightData(15, 118, true));
        column = new TableColumn(table, SWT.NONE);
        column.setText(COLUMN_HEADINGS[1]);
        column.setAlignment(SWT.LEFT);
        
        tableLayout.addColumnData(new ColumnWeightData(15, 118, true));
        column = new TableColumn(table, SWT.NONE);
        column.setText(COLUMN_HEADINGS[2]);
        column.setAlignment(SWT.LEFT);
        
        tableLayout.addColumnData(new ColumnWeightData(15, 118, true));
        column = new TableColumn(table, SWT.NONE);
        column.setText(COLUMN_HEADINGS[3]);
        column.setAlignment(SWT.LEFT);
        
        tableLayout.addColumnData(new ColumnWeightData(15, 118, true));
        column = new TableColumn(table, SWT.NONE);
        column.setText(COLUMN_HEADINGS[4]);
        column.setAlignment(SWT.LEFT);
        
        tableLayout.addColumnData(new ColumnWeightData(15, 118, true));
        column = new TableColumn(table, SWT.NONE);
        column.setText(COLUMN_HEADINGS[5]);
        column.setAlignment(SWT.LEFT);
      
        return composite;
    }
    
    public void initialize(Order order) {
    	this.order = order;
    	List<Invoice> invoiceList = InvoiceManager.getInstance().listInvoices(order);
    	Invoices invoices = new Invoices();
    	for (Invoice invoice : invoiceList) {
			invoices.addInvoices(invoice);
		}
    	this.invoices = invoices;
    	order.setInvoices(invoices);
    	this.order = order;
    	this.invoices = order.getInvoices();
    	tableViewer = new TableViewer(table);
        tableViewer.setColumnProperties(COLUMN_HEADINGS);
        tableViewer.setContentProvider(new TableContentProvider());
        tableViewer.setLabelProvider(new TableLabelProvider());
        tableViewer.setInput(invoices);
    }
     
	public class TableLabelProvider extends LabelProvider implements ITableLabelProvider {
	    public Image getColumnImage(Object element, int columnIndex) {
	        return null;
	    }

        public String getColumnText(Object element, int columnIndex) {
        	Invoice invoice = (Invoice) element;
        	
        	switch (columnIndex) {
            case 0:
                return Converter.convertToString(invoice.getDueDate());
            case 1:
                return String.valueOf(invoice.getAmount());
            case 2:
                return invoice.getCondition();
            case 3:
                return invoice.getType();
            case 4:
                return invoice.getStatus();
            case 5:
            	if (invoice.getPaymentDate() == null) {
            		return ""; //$NON-NLS-1$
            	}
            	return Converter.convertToString(invoice.getPaymentDate());
            }
            return null;
        }

	}

	public class TableContentProvider implements IStructuredContentProvider, PropertyChangeListener {
		
        public TableContentProvider() {
        	invoices = order.getInvoices();
        	invoices.addChangeListener(this);
        }
        
		public Object[] getElements(Object parent) {
           return invoices.getInvoices().toArray();
        }
        
        public void dispose() {
        	invoices.removeChangeListener(this);
        }

        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
        }

		
		public void propertyChange(PropertyChangeEvent arg0) {
			tableViewer.refresh();
		}
	}
}
